package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.TransMapper;
import com.hd.pzhwlxt.pojo.GoodsOrder;
import com.hd.pzhwlxt.pojo.GoodsPrice;
import com.hd.pzhwlxt.pojo.Trans;
import com.hd.pzhwlxt.service.GoodsOrderService;
import com.hd.pzhwlxt.service.GoodsPriceService;
import com.hd.pzhwlxt.service.TransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
@create: 2022-09-26 18:18
@author: 怀念天空
@Description: 
*/

@Service
@Transactional
public class TransServiceImpl extends ServiceImpl<TransMapper, Trans> implements TransService{

    @Autowired
    private TransMapper transMapper;

    @Autowired
    private GoodsOrderService goodsOrderService;

    @Autowired
    private GoodsPriceService goodsPriceService;

    @Override
    public List<Trans> getTransInfo() {
        return transMapper.getTransInfo();
    }

    @Override
    public Page<Trans> getTransInfoPage(Page<Trans> page) {
        return transMapper.getTransInfoPage(page);
    }

    @Override
    public boolean updateTransInfo(Trans trans) {
        //处理完订单，重置订单状态
        Integer orderId = trans.getOrderId();
        GoodsOrder order = goodsOrderService.getById(orderId);
        order.setOrderState(1);
        goodsOrderService.updateById(order);
        //库存数量更新
        QueryWrapper<GoodsPrice> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",order.getGoodsId());
        GoodsPrice goodsPrice = goodsPriceService.getOne(wrapper);
        goodsPrice.setStock(goodsPrice.getStock() - order.getNum());
        goodsPriceService.updateById(goodsPrice);
        QueryWrapper<Trans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        return transMapper.update(trans,queryWrapper) > 0;
    }

    @Override
    public List<Trans> getTransInfoByVehicleId(Integer id) {
        return transMapper.getTransInfoByVehicleId(id);
    }

    @Override
    public List<Trans> getTrainWayByRouteId(Integer id) {
        return transMapper.getTrainWayUsed(id);
    }

}
